import {EventEmitter, Injectable} from '@angular/core';
import {Http, RequestOptions} from "@angular/http";
import {Driver} from "../model/driver";
import {BackCode} from "../model/backCode";
import MTSConstants from "../config/config";

@Injectable()
export class DriverService {

  servicename:string="driverService";
  searchEvent: EventEmitter<string> = new EventEmitter();
  listFreshEvent: EventEmitter<string> = new EventEmitter();   //列表刷新标志
  searchFreshEvent: EventEmitter<string> = new EventEmitter();  //搜索框刷新标志
  constructor(private http: Http) { }

  createDriver(postvalue:Driver,fileoids: string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}driver`;
    //let body = JSON.stringify(val);//这个也可以
    const body = postvalue;//这个可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    if(headers.has("attachs"))
      headers.delete("attachs");
    if(fileoids==""){
      fileoids = "#";
    }
    headers.append("attachs",fileoids);
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('createDriver',error);});
  }

  updateDriver(postvalue:Driver,fileoids: string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}driver`;
    //let body = JSON.stringify(val);//这个也可以
    const body = postvalue;//这个可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    if(headers.has("attachs"))
      headers.delete("attachs");
    if(fileoids==""){
      fileoids = "#";
    }
    headers.append("attachs",fileoids);
    console.log(fileoids);
    const options = new RequestOptions({ headers: headers });
    return this.http.put(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('updateDriver',error);});
  }

  deleteDriver(oid:string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}driver/${oid}`;
    //let body = JSON.stringify(val);//这个也可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.delete(url, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('deleteDriver',error);});
  }

  getDriverList(): Promise<Driver[]>{
    const url = `${MTSConstants.HOSTUser}drivers`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json() as Driver[];})
      .catch((error: any) => {return this.handleError('getDriverList',error);});
  }

  getDriverByOid(oid:string){
    const url = `${MTSConstants.HOSTUser}driver/${oid}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json() as Driver;})
      .catch((error: any) => {return this.handleError('getDriverByOId',error);});
  }

  //判断司机ID是否重复
  checkDriverByID(id:string):Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}driverid/${id}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('getDriverById',error);});
  }


  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }
}
